package com.yuyou.fn.provider.impl;

import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import com.yuyou.fn.api.CourseClassService;
import com.yuyou.fn.api.dto.*;
import com.yuyou.fn.common.lang.Tuple2;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.educational.entity.ClassLecture;
import com.yuyou.fn.educational.service.IClassLectureService;
import com.yuyou.fn.educational.service.IPeriodService;
import com.yuyou.fn.portal.entity.CourseDisplayConfig;
import com.yuyou.fn.portal.entity.CourseFilterCondConfig;
import com.yuyou.fn.portal.entity.OuterCourseClass;
import com.yuyou.fn.portal.service.IConfigService;
import com.yuyou.fn.portal.service.IOuterCourseClassService;
import com.yuyou.fn.portal.vo.OuterCourseClassQueryVo;
import com.yuyou.fn.portal.vo.SimpleCourseClass;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class ExposeCourseClassServiceImpl implements CourseClassService {

    @Resource
    private IOuterCourseClassService outerCourseClassService;

    @Resource
    private IClassLectureService classLectureService;

    @Resource
    private IConfigService configService;

    @Override
    public PageDto<OuterCourseClassDto> queryCourseClass(CourseClassQueryParameterDto courseClassQueryParameter, int pageNo, int pageSize) {
        OuterCourseClassQueryVo outerCourseClassQueryVo = new OuterCourseClassQueryVo();
        BeanUtils.copyProperties(outerCourseClassQueryVo, courseClassQueryParameter );
        Tuple2<List<SimpleCourseClass>, Long> result = outerCourseClassService.findByConditionForPage(outerCourseClassQueryVo, pageNo, pageSize);
        Collection<OuterCourseClassDto> list  = Collections2.transform(result.getFirst(), this::toOuterCourseClassDto );
        return new PageDto<>(list ,pageNo,pageSize, result.getSecond().intValue() );
    }


    @Override
    public OuterCourseClassDto findCourseClassDetail(Long classId) {
        OuterCourseClass outerCourseClass = outerCourseClassService.findByClassId(classId);
        return toOuterCourseClassDto(outerCourseClass);
    }

    @Override
    public Collection<ClassLectureDto> findCourseLecture(Long classId) {
        List<ClassLecture> classLectures = classLectureService.findClassLectureByClassId(classId);
        if(classLectures == null ){
            return Collections.EMPTY_LIST;
        }
        return Collections2.transform(classLectures,this::toClassLectureDto);
    }

    @Override
    public List<PeriodDto> findAvailablePeriodForReg(Long areaTeamId) {

        CourseDisplayConfig courseDisplayConfig = configService.findCourseDisplayConfig(areaTeamId);
        if(courseDisplayConfig == null || courseDisplayConfig.getPeriods() == null  ){
            return Collections.EMPTY_LIST;
        }
        return Lists.transform(courseDisplayConfig.getPeriods(),this::toPeriodDto ) ;
    }

    private PeriodDto toPeriodDto(CourseDisplayConfig.Period period) {
        PeriodDto periodDto = new PeriodDto();
        periodDto.setPeriodId(period.getPeriodId());
        periodDto.setPeriodName(period.getPeriodName());
        return periodDto ;
    }

    private ClassLectureDto toClassLectureDto(ClassLecture classLecture) {
        ClassLectureDto classLectureDto = new ClassLectureDto() ;
        BeanUtils.copyProperties(classLectureDto,classLecture);
        return classLectureDto;
    }

    private OuterCourseClassDto toOuterCourseClassDto(Object courseClassResultVo) {
        OuterCourseClassDto courseClassDto = new OuterCourseClassDto() ;
        BeanUtils.copyProperties(courseClassDto, courseClassResultVo);
        return courseClassDto;
    }


}
